<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片式 -->
    <el-card>
      <!-- 检索 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <div>
            <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable class="input-with-select">
              <el-button slot="append" icon="el-icon-search" @click="searchGoods"></el-button>
            </el-input>
          </div>
        </el-col>
      </el-row>

      <!-- 订单列表 -->
      <el-table :data="orderData" border stripe style="width: 100%; margin-top: 15px">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="400px"></el-table-column>
        <el-table-column prop="order_price" label="订单价格(元)"></el-table-column>
        <el-table-column prop="pay_status" label="是否付款">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.pay_status === '0'">未付款</el-tag>
            <el-tag type="success" v-else-if="scope.row.pay_status === '1'">已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"></el-table-column>
        <el-table-column prop="create_time" label="下单时间" width="250px">
          <template slot-scope="scope">
            {{ scope.row.create_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="ordersEdit(scope.row)"></el-button>
            <el-button type="success" icon="el-icon-location-outline" size="mini" @click="orderAddress(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <div class="block" style="margin-top: 15px">
        <el-pagination @size-change="sizeChange" @current-change="currentChange" 
        :current-page="queryInfo.pagenum" :page-sizes="[1, 2, 5, 10]" :page-size="queryInfo.pagesize" 
        layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
    </el-card>

    <!-- 编辑对话框 -->
    <el-dialog title="编辑地址" :visible.sync="editDialogVisible" width="50%" :before-close="editDialogClose">
      <!-- body -->
      <el-form :model="addressForm"  :rules="addressFormRules" ref="addressFormRef" 
      label-width="100px" class="demo-addressForm" >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader :options="cityOptions" v-model="addressForm.address1" @change="changeProvince" change-on-select></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>

      <!-- footer -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogClose">取 消</el-button>
        <el-button type="primary" @click="editDialogSure">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 物流进度对话框 -->
    <el-dialog title="物流进度" :visible.sync="progressVisible" width="50%" :before-close="progressClose">
      <!-- body -->
      <el-timeline>
        <el-timeline-item v-for="(activity, index) in progressInfo" :key="index" 
        :timestamp="activity.time"> 
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>

      <!-- footer -->
      <span slot="footer" class="progress-footer">
        <el-button @click="progressClose">取 消</el-button>
        <el-button type="primary" @click="progressSure">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import cityOptions from '@/js/cityData2017element.js'
export default {
  data() {
    return {
      orderData: [],
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
      editDialogVisible: false,
      addressForm: {
        address1: [],
        address2: ''
      },
      addressFormRules: {
        address1: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' },
        ],
        address2: [
          { required: true, message: '请输入详细地址', trigger: 'blur' },
        ],
      },
      city: [],
      cityOptions: cityOptions,
      progressVisible: false,
      progressInfo: [],
      reverse: true,
      activities: [{
        content: '活动按期开始',
        timestamp: '2018-04-15'
      }, {
        content: '通过审核',
        timestamp: '2018-04-13'
      }, {
        content: '创建成功',
        timestamp: '2018-04-11'
      }]
    }
  },
  created() {
    this.getOrderData()
  },
  methods: {
    getOrderData() {
      this.$http.get('orders', {
        params: this.queryInfo
      }).then(res => {
        console.log(res.data)
        this.total = res.data.data.total
        this.orderData = res.data.data.goods
      })
    },
    searchGoods() {
    },
    sizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderData()
    },
    currentChange(newCurrent) {
      this.queryInfo.pagenum = newCurrent
      this.getOrderData()
    },
    ordersEdit(scopeRow) {
      this.editDialogVisible = true
    },
    orderAddress(scopeRow) {
      // 打开物流进度对话框，获取物流进度
      this.progressVisible = true
      this.$http.get('/kuaidi/1106975712662').then(res => {
        if(res.data.meta.status !== 200) {
          this.$message.error(res.data.meta.message)
        }else {
          console.log(res.data)
          this.progressInfo = res.data.data 
          this.$message.success(res.data.meta.message)
        }
      })
    },
    editDialogClose() {
      this.$refs.addressFormRef.resetFields()
      this.editDialogVisible = false
    },
    editDialogSure() {
      this.$refs.addressFormRef.validate((valid) => {
        if (valid) {
          // 发起请求
          // this.$http.
          this.$message.info("未开发")
        } else {
          this.$message.error("请填写表单必填项")
          return false;
        }
      });
    },
    changeProvince() { 
      // 绑定值改变时
      console.log(this.addressForm)
    },
    progressClose() {
      this.progressVisible = false
    },
    progressSure() {
      this.progressVisible = false
    },
  },
}
</script>
<style lang="scss" scoped>
  
</style>